const { execSQL } = require('../../db/mysql');

//获取商品列表方法
function getList() {
    const sql = 'select * from product';
    return execSQL(sql);
}

//前台获取商品列表方法
function getOnSaleList() {
    const sql = `select * from product where pdt_sale=1`;
    return execSQL(sql);
}


//获取详情数据方法
const getDetail = id => {
    const sql = `select * from product where pdt_id='${id}'`;
    return execSQL(sql);
}

//获取类型列表
const selectCategory = (productData={}) => {
    const keywords = productData.type.split('·');

    let sql = `select * from product where (pdt_type='${keywords[0]}' and pdt_sale=1) or (pdt_type='${keywords[1]}' and pdt_sale=1)`;

    return execSQL(sql);
}

//分类界面中的搜索
const searchCategory = (productData = {}) => {
    // const keyword = productData.selectKey;
    const keyword = productData.keyword;
    const types = productData.type.split('·');

    let sql = `select * from product where (pdt_type='${types[0]}' or pdt_type='${types[1]}') and (pdt_name like '%${keyword}%' or pdt_describe like '%${keyword}%') and pdt_sale=1`;  

    return execSQL(sql);
}


// 产品模糊搜索
const selectProduct = (productData={}) =>{
    // const keyword = productData.selectKey;
    const keyword = productData.keyword;

    let sql = `select * from product where pdt_name like '%${keyword}%'`;

    sql += ` or pdt_type like '%${keyword}%'`
    
    sql+=` or pdt_describe like '%${keyword}%'`
    
    return execSQL(sql);
}

//前台模糊搜索
const selectOnSaleProduct = (productData = {}) => {
    // const keyword = productData.selectKey;
    const keyword = productData.keyword;

    let sql = `select * from product where pdt_name like '%${keyword}%' and pdt_sale=1`;

    sql += ` or pdt_type like '%${keyword}%' and pdt_sale=1`

    sql += ` or pdt_describe like '%${keyword}%' and pdt_sale=1`

    return execSQL(sql);
}

//添加新产品
const createNewProduct = (productData = {}) => {
    const productName = productData.pdt_name;
    const productPrice = productData.pdt_price;
    const productNum = productData.pdt_num;
    const productType = productData.pdt_type;
    const productSale = productData.pdt_sale;
    const productPreview = productData.pdt_preview;
    const productDescribe = productData.pdt_describe;

    const sql = `insert into product (pdt_name, pdt_price, pdt_num, pdt_sale, pdt_type, pdt_preview, pdt_describe) values ('${productName}', ${productPrice}, ${productNum}, ${productSale}, '${productType}', '${productPreview}', '${productDescribe}')`;
    return execSQL(sql).then(insertedResult => {
        return {
            id: insertedResult.insertId
        }
    });
}

//更新产品
const updateProduct = (id, productData = {}) => {
    const productName = productData.pdt_name;
    const productPrice = productData.pdt_price;
    const productNum = productData.pdt_num;
    const productType = productData.pdt_type;
    const productSale = productData.pdt_sale;
    const productPreview = productData.pdt_preview;
    const productDescribe = productData.pdt_describe;

    const sql =
        `update product set pdt_name='${productName}', pdt_price='${productPrice}', pdt_num='${productNum}', pdt_type='${productType}', pdt_sale='${productSale}', pdt_preview='${productPreview}', pdt_describe='${productDescribe}' where pdt_id=${id}`;
    return execSQL(sql).then(updateResult => {
        if (updateResult.affectedRows > 0) {
            return true;
        }
        return false;
    }); 
}

//删除产品
const deleteProduct = id => {
    const sql = `delete from product where pdt_id=${id}`
    return execSQL(sql).then(deleteResult => {
        if (deleteResult.affectedRows > 0) {
            return true;
        }
        return false;
    })
}

//更改产品在售状态
const changeProduct = (id, productData = {}) => {
    const productSale = productData.pdt_sale;

    const sql = `update product set pdt_sale='${productSale}' where pdt_id=${id}`;
    return execSQL(sql).then(updateResult => {
        if (updateResult.affectedRows > 0) {
            return true;
        }
        return false;
    });
}

//导出方法
module.exports = { 
    getList,
    getOnSaleList,
    getDetail,
    createNewProduct,
    updateProduct,
    deleteProduct,
    selectProduct,
    selectOnSaleProduct,
    changeProduct,
    selectCategory,
    searchCategory
}

